package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.HydroDataBean;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class EcHydroAdapter extends RecyclerView.Adapter<EcHydroAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private HydroDataBean mData;
    private OnItemClickListener mOnItemClickListener = null;
    private int[] img1 = {
            R.mipmap.img_hydro1, R.mipmap.img_hydro6,
            R.mipmap.img_hydro3, R.mipmap.img_hydro4,
            R.mipmap.img_hydro5, R.mipmap.img_hydro7};

    private int[] img2 = {
            R.mipmap.img_hydro1_, R.mipmap.img_hydro6_,
            R.mipmap.img_hydro3_, R.mipmap.img_hydro4_,
            R.mipmap.img_hydro5_, R.mipmap.img_hydro7_};

    private boolean isTable = com.mingtu.common.utils.ScreenUtils.isTablet();

    public EcHydroAdapter(Context context, HydroDataBean mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ec, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view, isTable);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (isTable) {
            holder.ivPicture.setBackgroundResource(img2[position]);
        } else {
            holder.ivPicture.setBackgroundResource(img1[position]);
        }
        if (mData != null) {
            HydroDataBean.DataBean dataBean = mData.getData();
            if (dataBean != null) {
                String time = dataBean.getCollectTime();

                String yw = dataBean.getYw();//液位
                String szph1 = dataBean.getSzph1();//水质ph1
                String swd = dataBean.getSwd();//水温度
                String zd = dataBean.getZd();//浊度
                String rjy = dataBean.getRjy();//溶解氧
                String szddl = dataBean.getSzddl();//水质电导率

                if (isTable) {
                    if (position % 2 == 0) {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    } else {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    }
                } else {
                    if (position % 4 == 0 || (position + 1) % 4 == 0) {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    } else {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    }
                }


                holder.tvTime.setText(time + "");
                if (position == 0) {
                    holder.tvNum.setText(yw + "");
                    holder.tvUnit.setText("m");
                    holder.tvName.setText("液位");
                } else if (position == 1) {
                    holder.tvNum.setText(rjy + "");
                    holder.tvUnit.setText("mg/l");
                    holder.tvName.setText("溶解氧");
                } else if (position == 2) {
                    holder.tvNum.setText(szph1 + "");
                    holder.tvUnit.setText("");
                    holder.tvName.setText("水质PH");
                } else if (position == 3) {
                    holder.tvNum.setText(swd + "");
                    holder.tvUnit.setText("°C");
                    holder.tvName.setText("水温度");
                } else if (position == 4) {
                    holder.tvNum.setText(zd + "");
                    holder.tvUnit.setText("NTU");
                    holder.tvName.setText("浊度");
                } else if (position == 5) {
                    holder.tvNum.setText(szddl + "");
                    holder.tvUnit.setText("us/cm");
                    holder.tvName.setText("水质电导率");
                }
            }

        }

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return img1.length;
    }

    public void upData(HydroDataBean data) {
        mData = data;
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public LinearLayout layout;
        public ImageView ivPicture;
        public TextView tvNum;
        public TextView tvUnit;
        public TextView tvName;
        public TextView tvTime;

        public ActivityHolder(View itemView, boolean isTable) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            ivPicture = (ImageView) convertView.findViewById(R.id.iv_picture);
            tvNum = (TextView) convertView.findViewById(R.id.tv_num);
            tvUnit = (TextView) convertView.findViewById(R.id.tv_unit);
            tvName = (TextView) convertView.findViewById(R.id.tv_name);
            tvTime = (TextView) convertView.findViewById(R.id.tv_time);

            ViewGroup.LayoutParams lp = layout.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行2个，间隙是10dp
            int mWidth = (screenWidth - SizeUtils.dp2px(20)) / 2;
            int mHeight = mWidth * SizeUtils.dp2px(125) / SizeUtils.dp2px(178);

            if (isTable) {
                mWidth = (screenWidth - SizeUtils.dp2px(26)) / 3;
                mHeight = mWidth * SizeUtils.dp2px(181) / SizeUtils.dp2px(258);
            }

            lp.width = mWidth;
            lp.height = mHeight;
            layout.setLayoutParams(lp);
        }
    }

}
